﻿using TodoList.Data;
using TodoList.MinimalApi.DTOs;
using TodoList.MinimalApi.Mapper;

namespace TodoList.MinimalApi.Endpoints.TodoList;

public class Create : IEndpoint
{
	public void MapEndpoint(IEndpointRouteBuilder endpoints)
	{
		endpoints.MapPost("/api/todoList", async (ApplicationDbContext db, CreateTodoItemRequest request) =>
			{
				var item = request.ToEntity();
				db.TodoItems.Add(item);
				await db.SaveChangesAsync();
				return Results.Created($"/todoList/{item.Id}", item);
			})
			.WithName("CreateTodoItem")
			.WithTags(Tags.TodoList);
	}
}
